package org.itboys.mobile.controller.mobile.income;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.entity.mysql.income.MdlIncome;
import org.itboys.mobile.service.mysql.mobile.income.MdlMobileIncomeService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/25
 * 描述：迈迪龙_收益
 */
@Controller
@RequestMapping("/mdl/mobile/income")
@Api(value = "income",description = "收益")
public class MdlMobileIncomeController extends BaseController{
    @Autowired
    private MdlMobileIncomeService incomeService;

    /**
     * 获取用户收益
     * @param request
     * @param response
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getIncome", method= RequestMethod.POST)
    @ApiOperation(value="【管理版，用户版】获取收益。")
    public void getIncome(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            MdlIncome income=incomeService.getIncome(memberId);
            ResponseMessage.success(income);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/income/getIncome----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 分页获取用户积分记录
     * @param request
     * @param response
     * @param type
     * @param createTime_start
     * @param createTime_end
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getIncomeRecord", method= RequestMethod.POST)
    @ApiOperation(value="【管理版，用户版】获取收益记录。")
    public void getIncomeRecord(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = false, name = "type", value = "查询记录的类型 代理商查询自己的记录时传1，查询下属销售员的记录时传2， 其他用户不用传") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(required = false, name = "createTime_start", value = "查询参数。开始时间。默认为空") @RequestParam(value = "createTime_start", required = false) String createTime_start,
            @ApiParam(required = false, name = "createTime_end", value = "查询参数。结束时间。默认为空") @RequestParam(value = "createTime_end", required = false) String createTime_end,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<Map<String,Object>> result=incomeService.getIncomeRecord(request,memberId,type);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/income/getIncomeRecord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 获取用户提现记录
     * @param request
     * @param response
     * @param createTime_start
     * @param createTime_end
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getCashRecord", method= RequestMethod.POST)
    @ApiOperation(value="【管理版，用户版】获取提现记录。")
    public void getCashRecord(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = false, name = "createTime_start", value = "查询参数。开始时间。默认为空") @RequestParam(value = "createTime_start", required = false) String createTime_start,
            @ApiParam(required = false, name = "createTime_end", value = "查询参数。结束时间。默认为空") @RequestParam(value = "createTime_end", required = false) String createTime_end,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<Map<String,Object>> result=incomeService.getCashRecord(request,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/income/getCashedRecord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 申请提现
     * @param request
     * @param response
     * @param applyAmount
     * @param accountId
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/applyCash", method= RequestMethod.POST)
    @ApiOperation(value="【管理版】申请提现")
    public void applyCash(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = false, name = "applyAmount", value = "提现金额") @RequestParam(value = "applyAmount", required = true) BigDecimal applyAmount,
            @ApiParam(required = false, name = "accountId", value = "账号id") @RequestParam(value = "accountId", required = true) Long accountId,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("applyAmount", applyAmount);
            param.put("accountId", accountId);
            param.put("nonce", nonce);
            param.put("sign", sign);
            boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }
            Long memberId = AccountIdHoldUtils.getMemberId();
            incomeService.applyCash(memberId,applyAmount,accountId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/mobile/income/applyCash----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
